﻿using System;
using System.Web.UI.WebControls;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;
using Sunyi.Project.WebPoint.Resource;
using Sunyi.Project.WebPoint.WebControl;

namespace ActivitySystem.Web.Admin
{
    public partial class LogList : WebPointPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    InitializeControlValues();

                    Search();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(this, ex);
                }
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                Logger.DeleteAll();

                AuditLogger.Write(AuditLogOperationCode.Delete, this.GetType().Name, WebPointGlobalConstant.ClearAll);

                this.ShowClientMessage(WebResource.OP_ClearAllLogSuccessfuly);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                gvLog.PageIndex = 0;
                Search();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        protected void gvLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gvLog.PageIndex = e.NewPageIndex;
                Search();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        #region Private Method

        private void InitializeControlValues()
        {
            tbxInsertTimestamp.Text = DateTime.Now.ToShortDateString();
        }

        private void Search()
        {
            string level = ddlLevel.SelectedValue;
            string source = tbxSource.Text;
            string category = tbxCategory.Text;
            string eventId = tbxEventId.Text;
            DateTime insertTimestamp = DateTime.MinValue;

            try
            {
                insertTimestamp = DateTime.Parse(tbxInsertTimestamp.Text);
                tbxInsertTimestamp.BackColor = WebControlCommon.DefaultControlBackColor;
            }
            catch
            {
                this.ShowClientMessage(string.Format(WebResource.InvalidDateTimeField, tbxInsertTimestamp.ID));
                tbxInsertTimestamp.BackColor = WebControlCommon.WarningBackColor;
                return;
            }

            if (gvLog.PageIndex == 0)
            {
                gvLog.TotalCount = Logger.SearchCount(level, source, category, eventId, insertTimestamp);
            }

            var logs = Logger.Search(level, source, category, eventId, insertTimestamp, gvLog.PageIndex, gvLog.PageSize);

            gvLog.DataSource = logs;
            gvLog.DataBind();

            btnClear.Visible = logs.Count > 0;

            if (!Page.IsPostBack)
            {
                AuditLogger.Write(AuditLogOperationCode.Search, this.GetType().Name, level, source, category, eventId, insertTimestamp, gvLog.PageIndex, gvLog.PageSize);
            }
        }

        #endregion

        #region WebPointPage Member

        protected override void ApplyPermission()
        {
            if (!this.HasPermission(PermissionType.Delete))
            {
                btnClear.Visible = false;
            }
        }

        #endregion
    }
}